<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .tdContents {
            max-width: 200px;
            word-wrap: break-word;
        }
    </style>
</head>

<body>
    <div id="app">
        <!-- 用户输入评论 -->
        <textarea cols="30" rows="10" @blur="comment" :value="comment_text"></textarea>
        <button @click="sendcomment">发布评论</button>
        <!-- 渲染用户的评论 -->
        <table border="1" cellspacing="0" cellpadding="5">
            <tr>
                <th>ID</th>
                <th>游客</th>
                <th>评论</th>
                <th>点赞数</th>
                <th>发布日期</th>
                <th colspan="3">操作</th>
            </tr>
            <tr v-for="(item,index) in list">
                <!-- id -->
                <td><span>{{item.id}}</span></td>
                <!-- 游客 -->
                <td><span>{{item.name}}</span></td>
                <!-- 评论 -->
                <td class="tdContents"><span>{{item.contents}}</span></td>
                <!-- 点赞数 -->
                <td><span>{{item.count}}</span></td>
                <!-- 发布日期 -->
                <td><span>{{item.time}}</span></td>
                <!-- 点赞 -->
                <td><button @click="item.count++">点赞</button></td>
                <!-- 置顶 -->
                <td><button @click="totop(item,index)">置顶</button></td>
                <!-- 删除 -->
                <td><button @click="del(index)">删除</button></td>
            </tr>
        </table>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        let vm = new Vue({
            el: '#app',
            data: {
                list: [
                    {
                        id: 1,
                        name: '茄子',
                        contents: '住在山里面,真不错~',
                        count: 999,
                        time: '2021-10-11'
                    },
                    {
                        id: 2,
                        name: '大司马',
                        contents: '芜湖起飞~',
                        count: 0,
                        time: '2021-10-12'
                    },
                    {
                        id: 3,
                        name: 'PDD',
                        contents: '反向抽烟日神仙',
                        count: 0,
                        time: '2021-10-12'
                    },
                    {
                        id: 4,
                        name: '卢本伟',
                        contents: '伞兵一号卢本伟准备就绪',
                        count: 0,
                        time: '2021-10-12'
                    },
                    {
                        id: 5,
                        name: '鸡哥',
                        contents: '你干嘛~',
                        count: 0,
                        time: '2021-10-12'
                    },
                ],
                comment_text:"",
                number:0,
                id:2
            },
            methods: {
                comment(event){
                    this.comment_text = event.target.value
                },
                sendcomment(){
                    if(this.comment_text.length == 0){
                        alert("请输入内容")
                    }else{
                        let date = new Date()
                        let year = date.getFullYear();
                        let month = date.getMonth();
                        let day = date.getDate();
                        this.id++
                        this.list.push({id:this.id,name:"",contents:this.comment_text,count:this.number,time:year+"-"+month+"-"+day})
                        this.comment_text = ""
                        this.timenow = ""
                        this.number = 0
                    }
                    
                },
                del(index){
                    this.list.splice(index,1)
                },
                totop(item,index){
                    this.list.splice(index,1)
                    this.list.unshift(item)
                }
            }
        })
    </script>
</body>

</html>